
$(document).ready(function() {
	
	$('table#timetable td').click(function(event) {
		$('form input#groupName').val('');
		$('form input#timeSlot').val('');
	});
	
	$('table td.bookable').click(function(event) {
		var groupName = $(event.target).text();
		// var row = parseInt($(this).parent().prevAll().length) + 1;
        var col = $('table thead th:nth-child(' + parseInt($(this).prevAll().length + 1) + ')').text();
		var time = ($(this).prevAll().last().text());
		$('form input#groupName').val(groupName);
		$('form input#timeSlot').val(col + " klo " + time);
	});
	
	var errorNotificator = function(errorFieldId) {
		//  console.log('setting error not to: ' + errorFieldId)
		$('td#' + errorFieldId + 'Error').show();
	};
	
	$('form#regForm').submit(function() {
		
		var errors = false;
		$('td.errorNotification').hide();
		$('div#errorMsg').hide();
	
		if (!($('form input#timeSlot').val().length > 3)) {
			errorNotificator('timeSlot');
			errors = true;
		}
		
		if (!($('form input#groupName').val().length > 3)) {
			errorNotificator('groupName');
			errors = true;
		}

		if (!($('form input#email').val().length > 3)) {
			errorNotificator('email');
			errors = true;
		}

		if (!($('form input#firstName').val().length > 2)) {
			// console.log('adultname missing');
			errorNotificator('firstName');
			errors = true;
		}
		
		if (!($('form input#lastName').val().length > 2)) {
			// console.log('lastname missing');
			errorNotificator('lastName');
			errors = true;
		}
		
		if (!($('form input#puh').val().length > 5)) {
			// console.log('puh missing');
			errorNotificator('puh');
			errors = true;
		}
		
		if (errors) {
			$('div#errorMsg').show();
		}

		return !errors;
	});
});
	
